using System;
using System.Collections.Generic;
using System.Text;
using Forecast.Database;

namespace Forecast.Business
{
    internal class SelectedNews
    {
        private const int TOP_NEWS_ITEM_COUNT = 5;

        internal IList<NewsItem> GetLatest(IList<NewsItem> allNewsItems)
        {
            IList<NewsItem> latestNews = new List<NewsItem>();

            int newsCount = 0;
            foreach (NewsItem newsItem in allNewsItems)
            {
                if (newsItem.ShouldBeShownInMainNews)
                    continue;

                if (newsItem.Message.Contains("sold"))
                    continue;

                latestNews.Add(newsItem);
                newsCount++;
                if (newsCount == 3)
                    break;
            }

            return latestNews;
        }

        internal IList<NewsItem> GetTopMainNews(IList<NewsItem> allNewsItems)
        {
            IList<NewsItem> topNews = new List<NewsItem>();

            int newsCount = 0;
            foreach (NewsItem newsItem in allNewsItems)
            {
                if (!newsItem.ShouldBeShownInMainNews)
                    continue;

                topNews.Add(newsItem);
                newsCount++;
                if (newsCount == TOP_NEWS_ITEM_COUNT)
                    break;
            }

            return topNews;
        }

        internal IList<NewsItem> GetAllMainNews(IList<NewsItem> allNewsItems)
        {
            IList<NewsItem> mainNews = new List<NewsItem>();

            foreach (NewsItem newsItem in allNewsItems)
            {
                if (newsItem.ShouldBeShownInMainNews)
                {
                    mainNews.Add(newsItem);
                }
            }

            return mainNews;
        }

        internal IList<NewsItem> GetTopPredictionNews(IList<NewsItem> allNewsItems, Prediction prediction)
        {
            IList<NewsItem> topPredictionNews = new List<NewsItem>();

            int newsCount = 0;
            foreach (NewsItem newsItem in allNewsItems)
            {
                if (!newsItem.ShouldBeShownInMainNews)
                    continue;
                if (newsItem.PredictionId != prediction.Id)
                    continue;

                topPredictionNews.Add(newsItem);
                newsCount++;
                if (newsCount == TOP_NEWS_ITEM_COUNT)
                    break;                
            }

            return topPredictionNews;
        }

        internal IList<NewsItem> GetAllPredictionNews(IList<NewsItem> allNewsItems, Prediction prediction)
        {
            IList<NewsItem> predictionNews = new List<NewsItem>();

            foreach (NewsItem newsItem in allNewsItems)
            {
                if (!newsItem.ShouldBeShownInMainNews)
                    continue;
                if (newsItem.PredictionId != prediction.Id)
                    continue;
                
                predictionNews.Add(newsItem);
            }
         
            return predictionNews;
        }

        internal IList<NewsItem> GetTopPlayerNews(IList<NewsItem> allNewsItems, Player player)
        {
            IList<NewsItem> topPlayerNews = new List<NewsItem>();

            int newsCount = 0;
            foreach (NewsItem newsItem in allNewsItems)
            {
                if (newsItem.PlayerId != player.Id)
                    continue;

                topPlayerNews.Add(newsItem);
                newsCount++;
                if (newsCount == TOP_NEWS_ITEM_COUNT)
                    break;
            }

            return topPlayerNews;
        }

        internal IList<NewsItem> GetAllPlayerNews(IList<NewsItem> allNewsItems, Player player)
        {
            IList<NewsItem> playerNews = new List<NewsItem>();

            foreach (NewsItem newsItem in allNewsItems)
            {
                if (newsItem.PlayerId != player.Id)
                    continue;

                playerNews.Add(newsItem);
            }

            return playerNews;
        }
    }
}
